<%@ page language="java" import="java.util.*,java.text.*,java.io.*" pageEncoding="utf-8"%>
<%@page import="issuedfileaction.DBConnection;"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>下发文件</title>
    
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

  </head>
  
  <body>    
    <%
	  String IssuedFile=request.getParameter("Issuedfile");
	  IssuedFile=new String(IssuedFile.getBytes("ISO-8859-1"),"utf-8");  
	  String[] IssuedFileArray=IssuedFile.trim().split("@@");
	  SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
	  Properties propertyport = new Properties();
	  String ClassPath=DBConnection.class.getResource("/").getPath().trim()+"\\DB.properties";
	  ClassPath=ClassPath.replace("%20"," ");
	  DBConnection DB=new DBConnection();
	  
      propertyport.load(new FileInputStream(ClassPath));      
      String targetpath=propertyport.getProperty("EAI_Path");
      String splitpath=propertyport.getProperty("SPLIT_Path");//要分Inv,Pur,Sal  
      String saleread=propertyport.getProperty("sale");
      String purchaseread=propertyport.getProperty("purchase");
      String stockread=propertyport.getProperty("stock");
      String SQLStr="";
	  for(int i=0;i<IssuedFileArray.length;i++){
	      String[] splitarr=IssuedFileArray[i].split("!!");
	      File FileSource=new File(splitarr[0]);
	      File FileDest=null;
	      File FileDir=null;
	      splitpath=splitpath.replace("$CustomerCode",splitarr[3].trim().toUpperCase());
          if(saleread.trim().toUpperCase().contains(splitarr[4].trim().toUpperCase())){          
             splitpath=splitpath.replace("$BusinessType",saleread.split(",")[2].trim());
          }else if(purchaseread.trim().toUpperCase().contains(splitarr[4].trim().toUpperCase())){	            
             splitpath=splitpath.replace("$BusinessType",purchaseread.split(",")[2].trim());	         
          }else if(stockread.trim().toUpperCase().contains(splitarr[4].trim().toUpperCase())){	            
             splitpath=splitpath.replace("$BusinessType",stockread.split(",")[2].trim());	         
          }	 
          String FileFoldType=DB.SQLExec("select dt_mdealtype from dtmonitor where dt_mid='"+splitarr[1].trim()+"' ");        	         
	      if((FileFoldType.equals("1")) || (FileFoldType.equals("3")) || (FileFoldType.equals("2"))){
	         FileDest=new File(targetpath,splitarr[5].trim());
	         FileDir=new File(targetpath);
	      }else if(FileFoldType.equals("4")){
	         FileDest=new File(splitpath,splitarr[5].trim());
	         FileDir=new File(splitpath);
	      }
	      String lastModifyTime=sdf.format(new Date(FileSource.lastModified()));
	      if(!FileDir.exists()){
	         FileDir.mkdirs();
	      }
	      FileInputStream from=new FileInputStream(FileSource);
	      FileOutputStream to=new FileOutputStream(FileDest);
	      byte[] buffer=new byte[1024];
	      int bytes_read;
	      while((bytes_read=from.read(buffer))!=-1){
	         to.write(buffer,0,bytes_read);
	      }   
	      to.close();
	      from.close();	         
	      if((FileFoldType.equals("1")) || (FileFoldType.equals("3")) || (FileFoldType.equals("2")) ){
			 if(splitarr[2].trim().equals("4")){
				SQLStr="update dtmonitor set dt_mfiletype='5' where dt_mid='"+splitarr[1].trim()+"' ";					   
			 }else{
				SQLStr="update dtmonitor set dt_mfiletype='4' where dt_mid='"+splitarr[1].trim()+"' ";					   
			 }
	      }else if(FileFoldType.equals("4")){
			 if(splitarr[2].trim().equals("6")){
				SQLStr="update dtmonitor set dt_mfiletype='7' where dt_mid='"+splitarr[1].trim()+"' ";					   
			 }else{
				SQLStr="update dtmonitor set dt_mfiletype='6' where dt_mid='"+splitarr[1].trim()+"' ";					   
			 }
	      }
	      DB.UpdateExec(SQLStr);	      
	  }	  
	 HttpSession httpSession = request.getSession();          
     List<Object> ListRows = new ArrayList<Object>();
     SQLStr=httpSession.getAttribute("ReturnSQL").toString();
     ListRows=DB.SQLListExec(SQLStr);
     httpSession.setAttribute("filelist", ListRows);	
	 httpSession.setAttribute("beginDate", httpSession.getAttribute("beginDate").toString());
	 httpSession.setAttribute("endDate", httpSession.getAttribute("endDate").toString());
	 httpSession.setAttribute("dealercode", httpSession.getAttribute("dealercode").toString());
	 httpSession.setAttribute("dealername", httpSession.getAttribute("dealername").toString());	 
	 httpSession.setAttribute("filetype", httpSession.getAttribute("filetype").toString());
	 httpSession.setAttribute("ReturnSQL", SQLStr);
	 httpSession.setAttribute("alertvalue", "下发文件成功!");			 	    	       
     response.sendRedirect("CheckIssue.jsp"); 
    %>
  </body>
</html>



















































































